import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:vc_package/utils/base_color_utils.dart';
import 'package:vc_package/utils/base_sys_utils.dart';
import 'package:vc_package/widget/extension.dart';
import 'package:vc_package/widget/image_view.dart';

import '../utils/keyboard_overlay.dart';

class EditView extends StatefulWidget {
  final bool? obscureText;

  final String? text;
  final String? hintText;
  final String? errorText;
  final Color? hintTextColor;
  final double? hintTextSize;
  final Widget? suffixIcon;
  final Color? textColor;
  final double? textSize;

  final ValueChanged<String>? onChanged;

  final TextStyle? textStyle;

  final TextEditingController? controller;
  final TextInputType? keyboardType;

  final EdgeInsets? margin;
  final EdgeInsets? padding;

  final int? maxLength;
  final bool? autofocus;

  final String? src;
  final InputBorder? inputBorder;
  final int? maxLines;
  final double? radius;
  final Alignment? alignment;

  final VoidCallback? onClick;

  final bool? showBorder;
  final bool? showLine;
  final Color? lineColor;
  final TextAlign? textAlign;
  final FocusNode? focusNode;
  final VoidCallback? onEditingComplete;
  final ValueChanged<String>? onSubmitted;
  final bool? useDefaultEditController;
  final bool? editable;
  final bool? showLabelText;
  final bool? isDense;
  final TextInputAction? textInputAction;
  final bool? enableinteractiveSelection;
  final List<TextInputFormatter>? inputFormatters;
  final InputDecoration? inputDecoration;
  final VoidCallback? listener;

  const EditView({
    Key? key,
    this.text,
    this.hintText,
    this.errorText,
    this.textColor = ColorUtils.black,
    this.hintTextColor = ColorUtils.blackLiteLite,
    this.textSize = 14,
    this.hintTextSize = 14,
    this.autofocus = false,
    this.onChanged,
    this.suffixIcon,
    this.textStyle,
    this.controller,
    this.obscureText = false,
    this.keyboardType = TextInputType.emailAddress,
    this.src,
    this.padding = const EdgeInsets.all(0),
    this.margin = const EdgeInsets.all(0),
    this.inputBorder,
    this.maxLines = 1,
    this.radius = 3,
    this.alignment = Alignment.centerLeft,
    this.maxLength,
    this.onClick,
    this.showBorder = false,
    this.showLine = true,
    this.textAlign = TextAlign.left,
    this.focusNode,
    this.onEditingComplete,
    this.onSubmitted,
    this.useDefaultEditController = false,
    this.editable = true,
    this.showLabelText = false,
    this.inputFormatters,
    this.lineColor,
    this.textInputAction,
    this.isDense = false,
    this.enableinteractiveSelection = true,
    this.inputDecoration,
    this.listener,
  }) : super(key: key);

  @override
  EditViewState createState() => EditViewState();
}

class EditViewState extends State<EditView> {
  onClick() {}

  TextEditingController? controllerDefault;
  FocusNode? focusNode;

  @override
  void initState() {
    super.initState();
    controllerDefault = widget.controller;
    initController();
    focusNode = widget.focusNode ?? FocusNode();
    if (widget.keyboardType == TextInputType.numberWithOptions() ||
        widget.keyboardType == TextInputType.number ||
        widget.keyboardType == TextInputType.phone) {
      focusNode?.addListener(() {
        bool hasFocus = focusNode!.hasFocus;
        if (hasFocus) {
          KeyboardOverlay.showOverlay(context!);
        } else {
          KeyboardOverlay.removeOverlay();
        }
        widget.listener?.call();
      });
    } else {}
  }


  void initController() {
    if (((widget.useDefaultEditController == true) ||
        (true != widget.editable)) &&
        null == controllerDefault) {
      controllerDefault = TextEditingController(text: widget.text ?? '');
      print('flutter TextEditingController initState');
    }
  }

  @override
  void dispose() {
    if (((widget.useDefaultEditController == true) ||
        (true != widget.editable)) &&
        null != controllerDefault) {
      if (widget.controller != controllerDefault) {
        try {
          controllerDefault?.dispose();
        } catch (e) {
          print(e);
        }
      }
      controllerDefault = null;
      print('flutter TextEditingController dispose');
    }
    super.dispose();
    focusNode?.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      alignment: widget.alignment,
      child: Padding(
        padding: widget.margin ?? EdgeInsets.only(left: 0),
        child: Container(
          padding: widget.padding,
          decoration: true == widget.showBorder
              ? BoxDecoration(
              color: ColorUtils.white,
              borderRadius: BorderRadius.circular(widget.radius ?? 0),
              boxShadow: const [
                BoxShadow(
                    color: ColorUtils.whiteDark,
                    blurRadius: 2,
                    spreadRadius: .5)
              ])
              : null,
          child: Row(
            children: [
              Expanded(
                child: TextField(
                    onTap: widget.onClick ?? onClick,
                    enableInteractiveSelection:
                    widget.enableinteractiveSelection,
                    inputFormatters: widget.inputFormatters,
                    autofocus: widget.autofocus ?? false,
                    style: widget.textStyle ??
                        TextStyle(
                          color: widget.textColor,
                          fontSize: widget.textSize,
                        ),
                    autocorrect: false,
                    enabled: widget.editable,
                    onEditingComplete: widget.onEditingComplete,
                    onSubmitted: widget.onSubmitted,
                    focusNode: focusNode,
                    textAlign: widget.textAlign ?? TextAlign.start,
                    maxLength: widget.maxLength,
                    maxLengthEnforcement: MaxLengthEnforcement.none,
                    maxLines: widget.maxLines,
                    minLines: 1,
                    textInputAction:
                    widget.textInputAction ?? TextInputAction.done,
                    keyboardType: widget.keyboardType,
                    controller: controllerDefault,
                    onChanged: widget.onChanged,
                    obscureText: widget.obscureText ?? false,
                    cursorColor: ColorUtils.black,
                    decoration: widget.inputDecoration ??
                        ((widget.showLabelText == true)
                            ? InputDecoration(labelText: widget.hintText)
                            : InputDecoration(
                          // hasFloatingPlaceholder: false,
                            floatingLabelBehavior:
                            FloatingLabelBehavior.never,
                            isDense: widget.isDense,
                            icon: BaseSysUtils.empty(widget.src)
                                ? null
                                : ImageView(
                              width: 25,
                              height: 25,
                              src: widget.src ?? '',
                            ),
                            hintStyle: TextStyle(
                                color: widget.hintTextColor,
                                fontSize: widget.hintTextSize),
                            hintText: widget.hintText,
                            errorText: (widget.errorText?.isEmpty ?? true)
                                ? null
                                : widget.errorText,
                            labelStyle:
                            const TextStyle(color: Colors.yellow),
                            enabledBorder: InputBorder.none,
                            focusedBorder: InputBorder.none,
                            border: InputBorder.none))),
              ),
              widget.suffixIcon ?? Container(),
            ],
          ),
        ),
      ),
    );
  }
}
